package org.ctstudio.oa.duty.web;

import java.util.List;

import org.ctstudio.oa.duty.bean.Staff;
import org.ctstudio.oa.duty.bean.StaffQuery;
import org.ctstudio.oa.duty.dao.StaffMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/staff")
public class StaffController extends AbstractController {
  @Autowired
  private StaffMapper mapper;

  @RequestMapping(value = "/list.do")
  public String list(@ModelAttribute("query") StaffQuery query, Model model) {
    long total = mapper.count(query);
    model.addAttribute("total", total);
    if (total > 0) {
      List<?> list = mapper.query(query);
      model.addAttribute("list", list);
    }
    return "staff/list";
  }

  @RequestMapping(value = "/add.do", method = RequestMethod.GET)
  public String beforeAdd(Model model) {
    model.addAttribute("info", new Staff());
    model.addAttribute("op", "add");
    return "staff/newOrEdit";
  }

  @RequestMapping(value = "/add.do", method = RequestMethod.POST)
  public String onAdd(@ModelAttribute("info") Staff info, BindingResult result, Model model) {
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "staffId", "CANNOTEMPTY");
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "name", "CANNOTEMPTY");
    if (!result.hasErrors()) {
      Staff exists = mapper.getByStaffId(info.getStaffId());
      if (exists != null) {
        result.rejectValue("staffId", "EXISTS");
      }
    }

    if (result.hasErrors()) {
      model.addAttribute("op", "add");
      return "staff/newOrEdit";
    } else {
      mapper.add(info);
      return "redirect:/staff/list.do";
    }
  }

  @RequestMapping(value = "/edit.do", method = RequestMethod.GET)
  public String beforeEdit(Model model, @RequestParam long id) {
    model.addAttribute("info", mapper.getById(id));
    model.addAttribute("op", "edit");
    return "staff/newOrEdit";
  }

  @RequestMapping(value = "/edit.do", method = RequestMethod.POST)
  public String onEdit(@ModelAttribute("info") Staff info, BindingResult result, Model model) {
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "staffId", "CANNOTEMPTY");
    ValidationUtils.rejectIfEmptyOrWhitespace(result, "name", "CANNOTEMPTY");

    if (!result.hasErrors()) {
      Staff exists = mapper.getByStaffId(info.getStaffId());
      if (exists != null && !exists.getId().equals(info.getId())) {
        result.rejectValue("staffId", "EXISTS");
      }
    }

    if (result.hasErrors()) {
      model.addAttribute("op", "edit");
      return "staff/newOrEdit";
    } else {
      mapper.edit(info);
      return "redirect:/staff/list.do";
    }
  }

  @RequestMapping(value = "/del.do", method = RequestMethod.GET)
  public String beforeDel(Model model, @RequestParam long id) {
    model.addAttribute("info", mapper.getById(id));
    model.addAttribute("op", "del");
    return "staff/newOrEdit";
  }

  @RequestMapping(value = "/del.do", method = RequestMethod.POST)
  public String onDel(@RequestParam long id, Model model) {
    mapper.del(id);
    return "redirect:/staff/list.do";
  }
}
